package com.example.jdbctemplateplusexample.controller;
import java.math.BigDecimal;
import java.util.Date;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.jdbctemplateplusexample.repository.UserRepository;
import com.example.jdbctemplateplusexample.repository.arcapply.entity.ArcSearch;

import io.gitee.cofenghen.jdbctempleplus.BaseJdbcTemplePlus;
import io.gitee.cofenghen.jdbctempleplus.core.IOrmTemplate;
import io.gitee.cofenghen.jdbctempleplus.model.PageResult;
import io.gitee.cofenghen.jdbctempleplus.support.Condition;
import io.gitee.cofenghen.jdbctempleplus.support.Operator;
import io.gitee.cofenghen.jdbctempleplus.support.QueryCondition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

//@CrossOrigin
@RestController
@RequestMapping("/Test")
@Tag(name = "测试")
@Slf4j
public class TestController extends BaseJdbcTemplePlus {


private UserRepository userRepository;
    private IOrmTemplate ormOperations;

    public TestController(UserRepository userRepository, IOrmTemplate ormOperations) {
        this.userRepository = userRepository;

        this.ormOperations = ormOperations;

    }
    @DS("arcapply")
    @Operation(summary = "测试仓储层")
    @PostMapping("/queryById_testRepository")
    ///@Operation(summary = "测试编码")
    public ArcSearch  queryByIdRepository(BigDecimal id) throws IOException {




        var  result =   userRepository.queryById(id);

        if (result==null)
            throw  new RuntimeException("不存在数据！");
        return  result;
    }
    @DS("arcapply")
    @Operation(summary = "查下测试")
    @PostMapping("/queryById_test")
    ///@Operation(summary = "测试编码")
    public ArcSearch  queryById(BigDecimal id) throws IOException {

       var  result =   ormOperations.queryById(ArcSearch.class,id);

       if (result==null)
           throw  new RuntimeException("不存在数据！");
       return  result;
    }
    @DS("arcapply")
    @Operation(summary = "查下测试带条件")
    @PostMapping("/queryBySearchidRepository")
    ///@Operation(summary = "测试编码")
    public List<ArcSearch> queryBySearchidRepository(BigDecimal id) throws IOException {
        QueryCondition<ArcSearch> queryCondition=new QueryCondition<>();
        queryCondition.add(new Condition<ArcSearch>(ArcSearch::getSearchid, Operator.EQ,id));
        var  result =   userRepository.query(queryCondition);

        if (result==null)
            throw  new RuntimeException("不存在数据！");
        return  result;
    }
    @DS("arcapply")
    @Operation(summary = "查下测试带条件")
    @PostMapping("/queryBySearchid")
    ///@Operation(summary = "测试编码")
    public List<ArcSearch> queryBySearchid(BigDecimal id) throws IOException {
        QueryCondition<ArcSearch> queryCondition=new QueryCondition<>();
        queryCondition.add(new Condition<ArcSearch>(ArcSearch::getSearchid, Operator.EQ,id));
        var  result =   ormOperations.query(ArcSearch.class,queryCondition);

        if (result==null)
            throw  new RuntimeException("不存在数据！");
        return  result;
    }

    @Operation(summary = "测试分页")
    @PostMapping("/queryPageTest")
    ///@Operation(summary = "测试编码")
    @DS("arcapply")
    public PageResult<ArcSearch> queryPageTest(BigDecimal id) throws IOException {

        var  result =   ormOperations.ado().selectPageList("arc_search a","a.*",null,null,"searchid",null,null,null,1,10,"",ArcSearch.class);

        if (result==null)
            throw  new RuntimeException("不存在数据！");
        return  result;
    }
    @Operation(summary = "pg测试分页")
    @PostMapping("/queryPagePgTest")
    ///@Operation(summary = "测试编码")
    @DS("zctarc")
    public PageResult<Map<String,Object>> queryPagePgTest(BigDecimal id) throws IOException {

        var  result =   ormOperations.ado().selectPageMapList("zct_uc_user a","a.*",null,null,"userid",null,null,null,1,10,null);

        if (result==null)
            throw  new RuntimeException("不存在数据！");
        return  result;
    }

    @DS("arcapply")
    @Operation(summary = "测试数据库")
    @PostMapping("/test_data")
    ///@Operation(summary = "测试编码")
    public Long test_data() throws IOException {
        ArcSearch arcSearch=new ArcSearch();
        arcSearch.setSearchid(new BigDecimal("-1"));
        arcSearch.setSearchtype(2);
        arcSearch.setApplyid(null);
        arcSearch.setTransactor("1");
        arcSearch.setAccepttime(new Date());
        arcSearch.setApproveDate(new Date());
        arcSearch.setApproveState(0);
        arcSearch.setApproveUsername("ces");
        arcSearch.setSearchno("121");
        arcSearch.setCreatedate(new Date());
        arcSearch.setModifydate(new Date());
        arcSearch.setCreateuser("1");
        arcSearch.setModifyuser("1");
        arcSearch.setIsoldbusiness(0);
        arcSearch.setTransactorUserid(new BigDecimal("0"));
        arcSearch.setApproveUserid(new BigDecimal("0"));
        arcSearch.setRegoinid(0);
        arcSearch.setIssearchresult(0);
        arcSearch.setNodecaseid(new BigDecimal("0"));
        arcSearch.setVersionno(0L);
        arcSearch.setQueueno(null);
        arcSearch.setUnderlinestatus(0);
        arcSearch.setWsautoerrorstatus(0);
        arcSearch.setWsautoerrorcontent("");
        arcSearch.setWsautoerrordate(new Date());
        arcSearch.setSblshSw("");
        arcSearch.setYthsjmStatus(0);


        try {
            var result2 = ormOperations.insert(arcSearch);
            arcSearch.setApproveUsername("测试");
            var result3 = ormOperations.updateById(arcSearch.getSearchid(),arcSearch);
            log.info(result3+"");
        }
        catch (Exception ex)
        {
             ex.printStackTrace();
             throw  ex;
        }
     // var  result =  this.selectCloumn00("select  a.ts_id from t_aggregated_ts a where  rownum <2",null,Long.class);


        //   result =  this.commonSelectCloumn00("select  a.ts_id from t_aggregated_ts a where ts_id=? and  rownum <2", Long.class,1167L);

        return 1L;
    }





}
